//
//  TLSliderView.m
//  TLAVPlayer
//
//  Created by Tianlong on 2017/6/29.
//  Copyright © 2017年 Tianlong. All rights reserved.
//

#import "TLAVPlayerSlider.h"
#import "TLAVPlayerPublicFile.h"

/* 拖动按钮的宽度 */
#define kBtnWith 12
/* slider 的高度 */
#define kPlayProgressBarHeight 3

@interface TLAVPlayerSlider ()
/* 缓冲进度条、播放进度条，以及背景条，实际的长度 */
@property (nonatomic , assign) CGFloat viewWidth;
@end

@implementation TLAVPlayerSlider{
    
    UIView *_bgProgressView;         // 背景颜色
    UIView *_ableBufferProgressView; // 缓冲进度颜色
    UIView *_finishPlayProgressView; // 已经播放的进度颜色
    
    CGPoint _lastPoint;
    /** 进度圆球 */
    UIImageView *_progressBall;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        /* 背景 */
        _bgProgressView = [[UIView alloc] init];
        _bgProgressView.backgroundColor = [UIColor blackColor];
        [self addSubview:_bgProgressView];
        
        /* 缓存进度 */
        _ableBufferProgressView = [[UIView alloc] init];
        _ableBufferProgressView.backgroundColor = [UIColor yellowColor];
        [self addSubview:_ableBufferProgressView];
        
        /* 播放进度 */
        _finishPlayProgressView = [[UIView alloc] init];
        _finishPlayProgressView.backgroundColor = [UIColor redColor];
        [self addSubview:_finishPlayProgressView];
        
        /** 圆球 */
        _progressBall = [[UIImageView alloc] init];
        _progressBall.userInteractionEnabled = YES;
        _progressBall.layer.cornerRadius = kBtnWith*0.5;
        _progressBall.layer.masksToBounds = YES;
        _progressBall.backgroundColor = [UIColor whiteColor];
        [self addSubview:_progressBall];
        //_progressBall.hidden = YES;
        
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
        [self addGestureRecognizer:pan];
        
        self.dragging = NO;
        
        //self.backgroundColor = [UIColor blackColor];
    }
    return self;
}

#pragma mark - 拖拽进度条
-(void)panAction:(UIPanGestureRecognizer *)pan{
    switch (pan.state) {
        case UIGestureRecognizerStateBegan:
            [self beiginDraggingWithGestureRecognizer:pan];
            break;
            
        case UIGestureRecognizerStateChanged:
            [self didDraggingWithGestureRecognizer:pan];
            break;
            
        case UIGestureRecognizerStateEnded:
            [self endDraggingWithGestureRecognizer:pan];
            break;
            
        default:
            break;
    }
}

#pragma mark - layoutSubviews
-(void)layoutSubviews{
    [super layoutSubviews];
    
    WS(ws);
    [_bgProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.centerY.equalTo(ws.mas_centerY);
        make.right.offset(-kBtnWith);
        make.height.offset(kPlayProgressBarHeight);
    }];
    self.viewWidth = self.bounds.size.width;
}

#pragma mark - 设置各种条的背景色
/** 设置播放进度条的背景色 */
-(void)setPlayProgressBackgoundColor:(UIColor *)playProgressBackgoundColor{
    if (_playProgressBackgoundColor != playProgressBackgoundColor) {
        _finishPlayProgressView.backgroundColor = playProgressBackgoundColor;
    }
}

/** 设置缓冲进度条的背景色  */
-(void)setTrackBackgoundColor:(UIColor *)trackBackgoundColor{
    if (_trackBackgoundColor != trackBackgoundColor) {
        _ableBufferProgressView.backgroundColor = trackBackgoundColor;
    }
}

/** 设置底部背景进度条的背景色 */
-(void)setProgressBackgoundColor:(UIColor *)progressBackgoundColor{
    if (_progressBackgoundColor != progressBackgoundColor) {
        _bgProgressView.backgroundColor = progressBackgoundColor;
    }
}


#pragma mark - 设置播放进度
-(void)setCurTime:(float)curTime{
    
    _curTime = curTime;

    if(self.amountTime > 0){
        
        if (self.dragging){
            return;
        }
        
        float scale = 0.0f;
        //没有拖拽过进度
        if (self.amountTime > 0) {
            scale = self.curTime / self.amountTime;
        }
        
        WS(ws);
        [_finishPlayProgressView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(0);
            make.centerY.equalTo(ws.mas_centerY);
            make.right.offset(-kBtnWith-((ws.viewWidth-kBtnWith)*(1-scale)));
            make.height.offset(kPlayProgressBarHeight);
        }];
        
        [_progressBall mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_finishPlayProgressView.mas_right);
            make.centerY.equalTo(ws.mas_centerY);
            make.width.offset(kBtnWith);
            make.height.offset(kBtnWith);
        }];
    }
    else{
        //这个判断是为了先显示进度圆球
        WS(ws);
        [_progressBall mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(0);
            make.centerY.equalTo(ws.mas_centerY);
            make.width.offset(kBtnWith);
            make.height.offset(kBtnWith);
        }];
    }
}

#pragma mark - 设置缓冲时间
-(void)setBufferTime:(float)bufferTime{
    
    _bufferTime = bufferTime;
    
    if (self.amountTime > 0) {
        float scale = self.bufferTime / self.amountTime;
        
        WS(ws);
        [_ableBufferProgressView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(ws.mas_centerY);
            make.left.equalTo(ws.mas_left);
            make.right.equalTo(ws.mas_right).offset(-ws.viewWidth*(1 - scale)-kBtnWith);
            make.height.offset(kPlayProgressBarHeight);
        }];
    }
}

#pragma mark - 拖拽进度条
-(void)beiginDraggingWithGestureRecognizer:(UIPanGestureRecognizer *)pan{
    if ([self.delegate respondsToSelector:@selector(tlAVPlayerSlider:beiginDraggingForCurTime:)]) {
        [self.delegate tlAVPlayerSlider:self beiginDraggingForCurTime:self.curTime];
    }
    self.dragging = YES;
}

-(void)didDraggingWithGestureRecognizer:(UIPanGestureRecognizer *)pan{
    if (self.amountTime > 0) {
        
        CGPoint point = [pan locationInView:self];
        CGFloat offsetX = point.x;
        if (offsetX < 0 || offsetX > self.viewWidth - kBtnWith) {
            return;
        }
        
        WS(ws);
        [_finishPlayProgressView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(0);
            make.centerY.equalTo(ws.mas_centerY);
            make.width.mas_equalTo(offsetX);
            make.height.offset(kPlayProgressBarHeight);
        }];
        
        [_progressBall mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(offsetX);
            make.centerY.equalTo(ws.mas_centerY);
            make.width.offset(kBtnWith);
            make.height.offset(kBtnWith);
        }];
        
        if ([self.delegate respondsToSelector:@selector(tlAVPlayerSlider:didDraggingForScale:)]) {
            [self.delegate tlAVPlayerSlider:self didDraggingForScale:[self currentScale]];
        }
    }
}

-(void)endDraggingWithGestureRecognizer:(UIPanGestureRecognizer *)pan{
    
    float endDraggingTime = self.amountTime * [self currentScale];
    
    //先通知代理，改变传递的时间
    if ([self.delegate respondsToSelector:@selector(tlAVPlayerSlider:endDraggingForCurTime:scale:)]) {
        [self.delegate tlAVPlayerSlider:self endDraggingForCurTime:endDraggingTime scale:[self currentScale]];
    }
    
    self.dragging = NO;
}

#pragma mark - 获取当前播放的进度所占的比例
-(float)currentScale{
    //注意系数计算，进度条实际被覆盖的宽度 = self.viewWidth - kBtnWith*0.5
    float scale = 0.0f;
    if (self.viewWidth - kBtnWith > 0) {
        scale = _progressBall.frame.origin.x / (self.viewWidth - kBtnWith);
    }
    return scale;
}

-(void)dealloc {
    
}

@end
